import {createRouter, createWebHistory} from 'vue-router'

import {message} from 'ant-design-vue';
import {getUserToken} from '@/utils/cache';
import IndexPage from '../views/Home.vue';
export const routes = [
    // {
    //     path: '/',
    //     // 这段代码定义了根路径'/'的重定向行为
    //     // 当用户访问根路径'/'时，会执行这个重定向函数
    //     // window.location.href = '/home/clothings.html' 使用浏览器原生的方式进行页面跳转
    //     redirect: () => {
    //         window.location.href = '/home/index.html';
    //     }
    // },
    {
        path: '/',
        component: () => import('@/layouts/MainLayout.vue'),
        children: [
          
            // {

            //     path: '/home/index.html',
            //     name: 'Home',
            //     component: () => import("../views/Home.vue"),
            //     meta: {
            //         refresh: true,
            //     }
            // },
            // {
            //
            //     path: '/home/index.html',
            //     name: 'Home',
            //     component: () => import("../views/Home.vue"),
            //     meta: {
            //         refresh: true,
            //     }
            // },
            // {
            //     path: 'index',
            //     children: [
            //         {
            //             path: '',
            //             name: 'Home',
            //             component: () => import('../views/Home.vue'),
            //             meta: {requiresAuth: false}
            //         }
            //     ]
            // },
            {
                path: '/',
                name: 'Home',
                component: IndexPage,
            },
            {
                path: '/classify/:url',
                name: 'classify',
                component: () => import('../views/ProductClassify/index.vue'),
                meta: {
                    title: "Women's New Arrivals"
                }
            },
            {
                path: '/product',
                name: 'GoodDetails',
                component: () => import('../views/ProductDetails/index.vue'),
                meta: {
                    prerender: true
                }
            },
            {
                path: 'login',
                name: 'Login',
                component: () => import('../views/Login/Login.vue'),
            },
            {
                path: 'register',
                name: 'Register',
                component: () => import('../views/Register.vue'),
            },
            {
                path: 'product/:url',
                name: 'ProductDetail',
                component: () => import('../views/ProductDetails/index.vue'),
                meta: {
                    prerender: true
                }
            },
            {
                path: '/search',
                name: 'Search',
                component: () => import('../views/Search/index.vue'),
                meta: {
                    title: "Women's New Arrivals"
                }
            },
            {
                path: '/blog',
                name: 'Blog',
                component: () => import('../views/BlogPost/blog-post.vue'),
                meta: {
                    title: "Women's New Arrivals"
                }
            },
            {
                path: '/blog-left-menu',
                name: 'Blog-left-menu',
                component: () => import('../views/BlogPost/BlogLeftMenu.vue'),
                meta: {
                    title: "Women's New Arrivals"
                }
            },
            {
                path: 'featuredShops',
                name: 'FeaturedShops',
                component: () => import('../views/featuredShops/index.vue'),
            },
            {
                path: 'featuredClass',
                name: 'FeaturedClass',
                component: () => import('../views/featuredClass/index.vue'),
            },
            {
                path: 'sustainability',
                name: 'Sustainability',
                component: () => import('../views/Sustainability/index.vue'),
            },
            {
                path: '/privacypolicy',
                name: 'Privacypolicy',
                component: () => import('../views/Help/privacypolicy.vue')
            },
            {
                path: '/terms',
                name: 'Terms',
                component: () => import('../views/Help/terms.vue')
            },
            {
                path: '/cancelAccount',
                name: 'CancelAccount',
                component: () => import('../views/CancelAccount/index.vue')
            },
            {
                path: 'user',    //用户中心的 所有路由 都在这里都在这里
                // component: () => import('@/layouts/MainLayout.vue'),
                redirect: '/user/profile',
                meta: {requiresAuth: true},
                children: [
                    {
                        path: 'profile',
                        name: 'UserProfile',
                        component: () => import('@/views/user/ProfileView.vue'),
                        meta: {title: '个人资料', requiresAuth: true}
                    },
                    {
                        path: 'orders',
                        name: 'UserOrders',
                        component: () => import('@/views/user/OrdersView.vue'),
                        meta: {title: '我的订单', requiresAuth: true}
                    },
                    {
                        path: 'settings',
                        name: 'UserSettings',
                        component: () => import('@/views/user/SettingsView.vue'),
                        meta: {title: '账户设置', requiresAuth: true}
                    },
                    {
                        path: 'wishlist',
                        name: 'UserWishlist',
                        component: () => import('@/views/user/FavoritesView.vue'),
                        meta: {title: '我的收藏', requiresAuth: true}
                    },
                    {
                        path: 'address',
                        name: 'UserAddress',
                        component: () => import('@/views/user/AddressesView.vue'),
                        meta: {title: '我的地址', requiresAuth: true}
                    },
                    {
                        path: 'history',
                        name: 'UserHistory',
                        component: () => import('@/views/user/HistoryView.vue'),
                        meta: {title: '我的浏览', requiresAuth: true}
                    },
                    {
                        path: 'cart',
                        name: 'UserCart',
                        component: () => import('@/views/user/CartView.vue'),
                        meta: {title: '我的购物车', requiresAuth: true}
                    },
                    {
                        path: 'checkout',
                        name: 'Checkout',
                        component: () => import('@/views/user/CheckoutView.vue'),
                        meta: {title: '结算', requiresAuth: true}
                    },
                    {
                        path: 'points',
                        name: 'UserPoints',
                        component: () => import('@/views/user/PointsView.vue'),
                        meta: {title: '我的积分', requiresAuth: true}
                    },
                    {
                        path: 'coupons',
                        name: 'UserCoupons',
                        component: () => import('@/views/user/CouponsView.vue'),
                        meta: {title: '我的优惠券', requiresAuth: true}
                    },
                    {
                        path: 'message',
                        name: 'UserMessage',
                        component: () => import('@/views/user/NotificationsView.vue'),
                        meta: {title: '我的消息通知', requiresAuth: true}
                    }

                ]
            },
            {
                path: '/help',
                name: 'Help',
                component: () => import('../views/Help/index.vue'), // 当前这个组件
                children: [
                    {
                        path: 'returns/how-to-return',
                        component: () => import('../views/Help/Return/returns.vue')
                    },
                    {
                        path: 'returns/refunds-information',
                        component: () => import('../views/Help/Return/refunds.vue')
                    },
                    {
                        path: 'returns/key-returns-information',
                        component: () => import('../views/Help/Return/KeyReturnsInformation.vue')
                    },
                    {
                        path: 'account/signing-in-to-my-account',
                        component: () => import('../views/Help/MyAccount/SigningInToMyAccount.vue')
                    },
                    {
                        path: 'account/i-have-forgotten-my-password',
                        component: () => import('../views/Help/MyAccount/IHaveForgottenMyPassword.vue')
                    },
                    {
                        path: 'account/opening-an-account',
                        component: () => import('../views/Help/MyAccount/OpeningAnAccount.vue')
                    },
                    {
                        path: 'delivery/i-have-a-problem-with-my-delivery',
                        component: () => import('../views/Help/Delivery/IHaveAProblemWithMyDelivery.vue')
                    },
                    {
                        path: 'delivery/delivery-key-information',
                        component: () => import('../views/Help/Delivery/DeliveryKeyInformation.vue')
                    },
                    {
                        path: 'delivery/tracking-my-order',
                        component: () => import('../views/Help/Delivery/TrackingMyOrder.vue')
                    },
                    {
                        path: 'payments/help-with-payments',
                        component: () => import('../views/Help/Payments/HelpWithPayments.vue')
                    },
                    {
                        path: 'payments/what-payment-methods-can-i-use',
                        component: () => import('../views/Help/Payments/WhatPaymentMethodsCanIUse.vue')
                    },
                    {
                        path: 'shopping/has-my-order-gone-through',
                        component: () => import('../views/Help/Shopping/HasMyOrderGoneThrough.vue')
                    },
                    {
                        path: 'shopping/amend-cancel-my-order',
                        component: () => import('../views/Help/Shopping/AmendCancelMyOrder.vue')
                    }
                    ,
                    {
                        path: 'shopping/placing-an-order',
                        component: () => import('../views/Help/Shopping/PlacingAnOrder.vue')
                    },

                    {
                        path: 'other/i-have-a-complaint',
                        component: () => import('../views/Help/Other/IHaveAComplaint.vue')
                    },
                    {
                        path: 'other/sizing-guides',
                        component: () => import('../views/Help/Other/SizingGuides.vue')
                    },
                    {
                        path: 'other/contact-us',
                        component: () => import('../views/Help/Other/ContactUs.vue')
                    },


                ]
            },
            {
                path: '/:pathMatch(.*)*',
                redirect: '/'
            }
        ]
    }
]

const router = createRouter({
    scrollBehavior: () => ({left: 0, top: 0}),
    history: createWebHistory(),
    routes,
})

// 需要跳转到登录页的路径
const redirectToLoginPaths = ['/user'];

// 需要提示未登录但不跳转的路径
const showAlertPaths = ['/cart/checkout', '/product/buy'];

router.beforeEach((to, from, next) => {
    // 检查是否需要登录权限
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
    // 使用新的getUserToken函数检查token
    const hasToken = !!getUserToken();

    // 如果路由需要认证且没有token
    if (requiresAuth && !hasToken) {
        // 检查是否是需要直接跳转到登录页的路径
        const shouldRedirect = redirectToLoginPaths.some(path => to.path.startsWith(path)) ||
            to.path.startsWith('/user');

        // 检查是否是需要提示但不跳转的路径
        const shouldShowAlert = showAlertPaths.some(path => to.path.startsWith(path));

        if (shouldRedirect) {
            // 重定向到登录页
            next('/login');
        } else if (shouldShowAlert) {
            message.warning('Please log in before operation');
            // 停留在当前页面
            next(false);
        } else {
            // 其他需要认证的路径，默认跳转到登录页
            next('/login');
        }
    } else {
        // 不需要认证或已有token，正常导航
        next();
    }
})

export default router
